import tkinter#tk窗口库
import tkinter.messagebox#弹出提示框库
from tkinter import scrolledtext
import os#系统库
from tkinter import filedialog#文件选择操作
import tkinter.ttk#ttk美化库
from concurrent.futures import ThreadPoolExecutor,as_completed#多线程库
import threading#多线程库
from tqdm import *

from modules import list_remove
from modules import mcntomusic_start

class GUI:#main
    def __init__(self)->None:
        self.root = tkinter.Tk()
        self.root.title("NCM Tools By LingXiFox")#窗口标题
        self.root.geometry("1250x750")#窗口大小
        self.root.resizable(True,True)#允许自由拖拽窗口大小
        self.root.iconphoto(True, tkinter.PhotoImage(file='.\\pic.png'))#设置窗口图标
        self.interface()

    def interface(self):#GUI（窗口）
        def Get_Directory_Name():#选择文件夹
            directory = filedialog.askdirectory()
            return directory

        def file_extension(path):
            return os.path.splitext(path)[1]

        def scan_run(file_path:str):
            list_global = []
            list = os.listdir(file_path)
            for i in tqdm(range(0,len(list)),desc="扫描进度"):
                path = os.path.join(file_path, list[i])
                if os.path.isfile(path):
                    if os.path.isfile(path):
                        if file_extension(path) == ".ncm":
                            try:
                                list_global.append(path)
                            except:
                                pass
            return list_global

        def Scan_Files(folder_path:str)->list:#扫盘，遍历目录
            file_list = []
            for root, dirs, files in os.walk(folder_path):
                for file_name in files:
                    file_path = os.path.join(root, file_name)
                    file_list.append(file_path)
            return file_list

        def Read_Files_To_TextBox():#将文本输送到文本框里
            File_Path = Entry_Cloise_Files.get()
            Result = Scan_Files(File_Path)
            Result = str(Result)
            Result = list_remove.STR_REMOVE(Result)
            Text_Box_Left.config(state=tkinter.NORMAL)
            Text_Box_Left.insert(tkinter.END,Result)
            Text_Box_Left.config(state=tkinter.DISABLED)

        def When_Use_Button_CF():#选择文件夹时候的激活函数
            File_Location = ""
            File_Location = Get_Directory_Name()
            Entry_Cloise_Files.config(state="normal")
            Entry_Cloise_Files.insert(0,str(File_Location))
            Entry_Cloise_Files.config(state="readonly")
            Read_Files_To_TextBox()

        def When_Use_Button_Start():
            Files_Path = Entry_Cloise_Files.get()
            mcntomusic_start.run(str(Files_Path))

        Label_Choise_Files = tkinter.Label(self.root,text="选择包含NCM文件的文件夹：",font="20")
        Label_Choise_Files.place(x=10,y=10)
        Entry_Cloise_Files = tkinter.Entry(self.root,font="20",width=60,relief="solid",justify=tkinter.LEFT)
        Entry_Cloise_Files.place(x=260,y=10)
        Entry_Cloise_Files.config(state="readonly")
        Button_Choise_Files = tkinter.Button(self.root,text="选择文件夹",font="20",justify=tkinter.CENTER,command=When_Use_Button_CF)
        Button_Choise_Files.place(x=880,y=5)

        Label_Show_Files_Left = tkinter.Label(self.root,text="即将处理的文件",font="20")
        Label_Show_Files_Left.place(x=10,y=60)
        Text_Box_Left = scrolledtext.ScrolledText(self.root, wrap=tkinter.WORD,font="15")#文本框
        Text_Box_Left.place(x=10, y=100, width=620, height=200)
        Text_Box_Left.config(state=tkinter.DISABLED)

        Label_Show_Files_Right = tkinter.Label(self.root,text="已经处理的文件",font="20")
        Label_Show_Files_Right.place(x=630,y=60)
        Text_Box_Right = scrolledtext.ScrolledText(self.root, wrap=tkinter.WORD,font="15")#文本框
        Text_Box_Right.place(x=630, y=100, width=620, height=200)
        Text_Box_Right.config(state=tkinter.DISABLED)

        Button_Start = tkinter.Button(self.root,text="开始转换",font="20",justify=tkinter.CENTER,command=When_Use_Button_Start)
        Button_Start.place(x=1000,y=5)




if __name__ == "__main__":#程序入口
    a = GUI()
    a.root.mainloop()#窗口循环
    print("进程已退出！")